package com.asmember;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class SocketRequest implements Runnable{

	private String host;
	private Map<String, List<String>> connectedHosts;
	private List<String> ports;
	
	public SocketRequest(String host, Map<String, List<String>> connectedHosts, List<String> ports) {
		this.host = host;
		this.connectedHosts = connectedHosts;
		this.ports=ports;
	}
	
	@Override
	public void run() {
		
		for(String port: ports){
		
			System.out.println("Connection to "+host+" on port "+port );

			Socket socket = new Socket();
			InetSocketAddress endPoint = new InetSocketAddress( host,  
                                               Integer.parseInt( port )  );

			if ( !endPoint.isUnresolved() ) {
				try { 
					socket.connect(  endPoint , 2000 );
					System.out.println("Connection to host: "+host+" is success");
					connectedHosts.get(port).add(host);
        	}             
        	catch( IOException ioe ) {


        	} finally {

        		if ( socket != null ) try {
        			socket.close();
        		} catch( IOException ioe ) {}

        	} 

			}
        }
    }

}
